package net.qiyuesuo.service.impl;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

import net.qiyuesuo.pojo.FileMeta;
import net.qiyuesuo.service.AbstractService;
import net.qiyuesuo.service.StorageService;
import net.qiyuesuo.utils.RandomUtils;

public class StorageServiceImpl extends AbstractService implements StorageService {

	private String basePath = "C:\\Users\\Administrator\\Desktop\\File";
	
	@Override
	public FileMeta storageFile(InputStream input, long size) {
		String fileKey = RandomUtils.getRandomFileKey();
		Path path =  getFileByKey(fileKey);
		try(OutputStream output = new FileOutputStream(path.toFile())){
			//将文件输出到指定文件夹
			int len = -1;
			byte[] bt = new byte[1024];
			while((len=(input.read(bt)) )!= -1)   {
				output.write(bt, 0, len);
			}
			output.flush();
			logger.info("StorageService存储文件：将uuid为："+fileKey+"的数据输出到"+path.toFile().getAbsolutePath()+"中");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new FileMeta(fileKey,path.toFile().length(),new Date());
	}

	@Override
	public FileMeta storageFile(byte[] bytes) {
		ByteArrayInputStream input = new ByteArrayInputStream(bytes);
		return storageFile(input,bytes.length);
	}

	@Override
	public FileMeta storageFile(Path path) {
		FileMeta info = null;
		try (InputStream input = new FileInputStream(path.toFile())){
			info =storageFile(input,path.toFile().length());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return info;
	}

	/**
	 * 根据文件Key ,获取该文件对应路径，若不存在，则创建
	 * @param fileKey
	 * @return
	 */
	@Override
	public Path getFileByKey(String fileKey) {
		String firstDirString = basePath+"//"+fileKey.substring(0,2);
		String secondDirString = firstDirString +"//"+fileKey.substring(2, 4);
		Path path = null;
		try {
			if(!Files.exists(Paths.get(firstDirString),new LinkOption[]{ LinkOption.NOFOLLOW_LINKS})) {
				//检测时不包含符号链接文件
				Files.createDirectory(Paths.get(firstDirString));
			}
			if(!Files.exists(Paths.get(secondDirString), new LinkOption[]{ LinkOption.NOFOLLOW_LINKS})) {
				Files.createDirectory(Paths.get(secondDirString));
			}
			if(!Files.exists(Paths.get(secondDirString+"//"+fileKey), new LinkOption[]{ LinkOption.NOFOLLOW_LINKS})) {
				//文件不存在，创建文件
				path = Files.createFile(Paths.get(secondDirString+"//"+fileKey));
			}else {
				path = Paths.get(secondDirString+"//"+fileKey);
				//文件存在 返回该文件
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("读取uuid为："+fileKey+"的文件");
		return path;
	}

	@Override
	public void deleteFile(String uuid) {
		Path path = Paths.get(basePath+"//"+uuid.substring(0,2)+"//"+uuid.substring(2, 4)+"//"+uuid);
		path.toFile().delete();
		logger.info("storageService文件存储：删除uuid为："+uuid+"的文件");
	}
	
}
